'use strict';

define(function(require,exports,module){
	var React = require('react');
	var $ = require('jquery');
	//var bootstrap = require('bootstrap');
	var XDate = require('xdate');
	var xdate = new XDate();
	var jPages = require('jPages');

	/*DATE下拉框*/
	var SelectOptionDate = require('components/partial/SelectOptionDate');

	var publicObj = require('lib/public');
	var token = publicObj.get_token();

    //Pager
    var Pager = require('components/partial/Pager');
	var DataForm = require('./OauthTableList/DataForm');
	var SearchDataForm = require('./OauthTableList/SearchDataForm');
	var DataList = require('./OauthTableList/DataList');

	module.exports = React.createClass({displayName: "exports",
		showDataForm:function(){
			$('#myModal').modal();
		},
		searchData:function(keys){
			this.setState({'keys':keys});
			this.listData(1,keys);
			return;
		},
        listData:function(p,keys){
            p = parseInt(p);
            var page = p - 1;
            page = page > 0 ? page : 0;
            var pagesize = 100;
            var limit = " limit "+(page * pagesize)+","+pagesize+" ";

			
            var data = new Object();
			if(keys){
				data.sql = "select client_id,client_secret,redirect_uri from qeeniao.oauth_clients where (client_id like '%"+keys+"%' or client_secret like '%"+keys+"%' or redirect_uri like '%"+keys+"%') "+limit;
			}else{
				
				if(this.state.keys){
					data.sql = "select client_id,client_secret,redirect_uri \
					from qeeniao.oauth_clients \
					where (client_id like '%"+this.state.keys+"%' or client_secret like '%"+this.state.keys+"%' or redirect_uri like '%"+this.state.keys+"%') "+limit;
				}else{
					data.sql = "select client_id,client_secret,redirect_uri from qeeniao.oauth_clients "+limit;
				}
			}
            data.access_token = token;

            $.ajax({
                url:this.props.url,
                data:data,
                type:'post',
                dataType:'json',
                success:function(o){
                    if(this.isMounted()) {
                        this.setState({'data': o});

                        if (o.length < pagesize) {
                            this.setState({'next_stop': true});
                        } else {
                            this.setState({'next_stop': false});
                        }

                        if ((p - 1) <= 0) {
                            this.setState({'pre_stop': true});
                        } else {
                            this.setState({'pre_stop': false});
                        }

                        this.setState({page: p});
                    }

                }.bind(this),
                error:function(xhr,status,err){
                    console.error('获取数据失败!',status,err.toString());
                }.bind(this)
            })
        },
        loadDataSum:function(){
            var token = publicObj.get_token();
            var sql = "select count(*) as sum from qeeniao.oauth_clients";
            var data = new Object();
            data.sql = sql;
            data.access_token = token;

            $.ajax({
                url:this.props.url,
                data:data,
                type:'post',
                dataType:'json',
                success:function(o){
                    if(this.isMounted()) {
                        this.setState({'data_sum': o[0].sum});
                    }

                }.bind(this),
                error:function(xhr,status,err){
                    console.error('获取数据失败!',status,err.toString());
                }.bind(this)
            })
        },
        getInitialState:function(){
            var data = [];
            return {
                data:[],
                data_sum:0,
                date_data:[],
                date:'',
                page:1,
                pages:0,
                pre_stop:true,
                next_stop:false,
				keys:"",
            };

        },
        componentDidMount:function(){
            this.listData(1);
            this.loadDataSum();
        },
        render:function(){
            var data_list = this.state.data.map(function(user){
                return (
                    React.createElement("tr", null, 
                        React.createElement("td", null, user.client_id), 
                        React.createElement("td", null, user.client_secret), 
                        React.createElement("td", null, user.redirect_uri)
                    )

                )
            });

            var pager_props = {
                page : this.state.page,
                pre_stop:this.state.pre_stop,
                next_stop:this.state.next_stop,
                listData : this.listData
            };

            return (
                React.createElement("div", {className: "panel panel-default", style: {marginTop:10+'px'}}, 
                    React.createElement("div", {className: "panel-heading"}, 
                        React.createElement("div", {className: "panel-title"}, 
                            React.createElement("h4", {className: ""}, 
                                this.props.title, " :", 
                                React.createElement("span", {className: "red"}, this.state.data_sum, "个")
                            )
                            
                        )
                    ), 
					React.createElement("div", {className: "panel-body"}, 
						React.createElement(SearchDataForm, {id: "myModal", searchHandle: this.searchData, createHandle: this.showDataForm}), 
						React.createElement(DataForm, {id: "myModal"})
					), 
				
                    React.createElement("div", {className: "panel-body"}, 
                        React.createElement(DataList, {TrDataList: data_list})
                    ), 
							
                    React.createElement("div", {className: "panel-body"}, 
                        React.createElement(Pager, React.__spread({},  pager_props))
                    )
                )
            );
        }
	});
});